
/**
 * xugy
 **/
/**
 *
 * 修改特殊顺序 
 *
 **/
function updateSequence() {
	var sequence = jQuery(this).val();
	if (isNullOrEmpty(sequence) || isNaN(sequence) || sequence > 127 || sequence < -128) {
		alert("非法数据,顺序将被重置为0");
		jQuery(this).val(sequence = 0);
	}
	var param = {"id":jQuery(this).parents("tr").find(":checkbox").val(), "specialSequence":sequence};
	DWREngine.setAsync(false);
	productService.updateRecord("product_sqlMap.updateSpecialSequence", param, {callback:function (returnValue) {
		if(returnValue == 0){
			alert("修改失败");			
		}
	}});
}
/**
 *
 * 修改特惠价格
 *
 */
function updateSpecialPrice(id) {
	var array = jQuery("tr:has(input#chkSel[value=" + id + "]) td");
	var specialPrice = window.prompt("\u8bf7\u8f93\u5165:" + array[1].getElementsByTagName("a")[0].innerHTML + " \u7684\u65b0\u7279\u60e0\u4ef7\u683c", array[7].innerHTML);
	if (isNullOrEmpty(specialPrice) || isNaN(specialPrice) || specialPrice >= 1000000 || !checkNumber(5,specialPrice)) {
		return;
	}
	var specialTaxprice = array[8].innerHTML;
	specialTaxprice = window.prompt("\u8bf7\u8f93\u5165:" + array[1].getElementsByTagName("a")[0].innerHTML + " \u7684\u65b0\u7279\u60e0\u5f00\u7968\u4ef7\u683c", isNullOrEmpty(specialTaxprice) ? getTaxPrice(specialPrice) : specialTaxprice);
	if (isNullOrEmpty(specialTaxprice) || isNaN(specialTaxprice) || specialTaxprice >= 1000000 || !checkNumber(5,specialTaxprice)) {
		return;
	}
	var param = {"id":id, "specialPrice":specialPrice, "specialTaxprice":specialTaxprice};
	DWREngine.setAsync(false);
	productService.updateRecord("product_sqlMap.updateSpecialPrice", param, {callback:function (returnValue) {
		synchroizedShow(returnValue, array[7], specialPrice);
		synchroizedShow(returnValue, array[8], specialTaxprice);
	}});
}
function synchroizedShow(returnValue, target, value) {
	if (returnValue == 0) {
		return;
	}
	target.innerHTML = value;
}
function deletes() {
	var ids = new Array();
	jQuery("tr:has(:checkbox[name=chkSel]:checked)").each(function () {
		ids.push(jQuery(this).remove().find(":checkbox[name=chkSel]").val());
	});
	if (isNullOrEmpty(ids)) {
		return;
	}
	DWREngine.setAsync(false);
	productService.updateRecord("product_sqlMap.updateSpecialType", {"ids":ids.join(","), "specialType":0}, {callback:function (returnValue) {
		if (returnValue == 0) {
			alert("\u5220\u9664\u5931\u8d25!");
		}
	}});
}
function openSpecialSelector(tableID, specialType) {
	var ids = new Array();
	jQuery(":checkbox[name=chkSel]").each(function () {
		ids.push(jQuery(this).val());
	});
	var records = dialog.showModalDialog(jsCtx+"/product/productMessage.do?method=ProductList&forward=product.specialSelector&product.specialType=0&product.ids=" + ids.join(","), [self, specialType], '700','460');
	if (records == undefined || records.length == 0) {
		return;
	}
	for (var i = 0; i < records.length; i++) {
		var id = records[i].id ,name = records[i].name;
		var specialPrice = jQuery("<a>\u4fee\u6539\u7279\u60e0\u4ef7\u683c </a>").attr("href", "javascript:updateSpecialPrice(" + records[i].id + ");").hide();
		if (specialType == 2) {
			specialPrice.show();
		}
		jQuery("table#" + tableID + " tr#operator").before(jQuery(records[i].html).append(jQuery("<td></td>").addClass("td_03").append(jQuery("<a>\u7f16\u8f91\u8d44\u6599 </a>").attr("href", jsCtx+"/product/productMessage.do?method=ToUpdateProduct&id=" + id + "&forward=ToSpecialType")).append(specialPrice).append(jQuery("<a>\u5230\u8d27\u901a\u77e5\u540d\u5355 </a>").attr("href", "javaScript:void(0);").bind("click",function(){toShowProductnotify(id, name);})))).prev().children("td#specialSequence").html("").prepend(jQuery("<input size='5' maxlength='4'>").val(records[i].sequence).change(updateSequence));
	}
}
function specialResult() {
	var arr_chk = jQuery(":checkbox[name=chkSel]:checked"), records = new Array(), ids = new Array();
	if (arr_chk.length == 0) {
		return;
	}
	arr_chk.each(function () {
		ids.push(jQuery(this).val());
	});
	DWREngine.setAsync(false);
	productService.updateRecord("product_sqlMap.updateSpecialType", {"ids":ids.join(","), "specialType":window.dialogArguments[1]}, {callback:function (returnValue) {
		if (returnValue > 0) {
			jQuery("tr:has(:checkbox[name=chkSel]:checked)").each(function () {
				jQuery(this).children("[special*=" + window.dialogArguments[1] + "]").show();
				records.push({"id":jQuery(this).find(":checkbox[name=chkSel]").attr("checked", false).val(),"name":jQuery(this).children("#name").html(),"sequence":jQuery(this).children("#specialSequence").html(), "html":jQuery(this)[0].outerHTML});
			});
		} else {
			alert("\u6dfb\u52a0\u5931\u8d25!");
		}
	}});
	window.returnValue = records;
	window.close();
}

/*
	5000-9999元的，开票加200元
	10000-19999元的，开票加300元
	20000-49999元的，开票加500元
	50000-99999元的，开票加1000元
	100000元-199999元的，开票加2000元
	200000元-400000元，开票加4000元。
*/
function getTaxPrice(price){
	var price =parseInt(price);
	
	if(5000<=price && price<=9999){
		return parseInt(price)+parseInt(200);
	}else if(10000<=price && price<=19999){
		return parseInt(price)+parseInt(300);
	}else if(20000<=price && price<=49999){
		return parseInt(price)+parseInt(500);
	}else if(50000<=price && price<=99999){
		return parseInt(price)+parseInt(1000);
	}else if(100000<=price && price<=199999){
		return parseInt(price)+parseInt(2000);
	}else if(200000<=price && price<=400000){
		return parseInt(price)+parseInt(4000);
	}else if(price<5000 || price>400000){
		alert('此商品开具手写票（税点统一加4%）');
		return price+price*0.04;
	}
}

